<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of VA_Form_Validator_DateRange
 *
 * @author chaporgin_a
 */
class VA_Form_Validator_DateRange extends VA_Form_Validator {
  protected $min = null;
  protected $max = null;
  protected $messages = array(
          'Введите дату от %s до %s'
          , 'Введите дату не позднее %s'
          , 'Введите дату не ранее %s'
  );
  public function init(array $options) {
    parent::init($options);
    $this->min = $this->getOption('min', 0);
    if(0 !== $this->min) $this->min = strtotime($this->min);
    $this->max = $this->getOption('max', 0);
    if(0 !== $this->max) $this->max = strtotime($this->max);
  }
  public function isValid($value) {
    throw new Exception('not tested');
    $result = true;
    $value = strtotime($value);
    if( ($min !== 0) && ($min > $value) ) $result = FALSE;
    if( ($max !== 0) && ($max < $value)) $result = FALSE;
    if( FALSE === $result)
      return $result;
  }
  protected function provideErrorMessage($value) {
    if(($this->min !== 0) && ($this->max !== 0))
      return sprintf($this->messages[0], $this->min, $this->max);
    elseif(($this->min !== 0))
      return sprintf($this->messages[2], $this->min);
    elseif(($this->max !== 0))
      return sprintf($this->messages[1], $this->max);
    else throw new Exception('Validator needs some initialization');
  }
}

